package kordegi.forumsystem.service.Impl;

import kordegi.forumsystem.entity.Comment;
import kordegi.forumsystem.mapper.CommentMapper;
import kordegi.forumsystem.service.CommentService;
import kordegi.forumsystem.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author kordegi
 * @date 2022.05.06 20:14
 * @Description
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;

    @Autowired
    JwtUtils jwtUtils;

    /**
     * @param articleId
     * @return
     */
    @Override
    public List<Comment> getAllCommentList(Long articleId) {
        return commentMapper.getAllCommentList(articleId);
    }

    /**
     * @param comment
     * @return
     */
    @Override
    public boolean deleteCommentById(Comment comment) {
        int i = 0;
        try {
            int count = commentMapper.selectCount(comment.getId(), comment.getParentId());
            if (count > 0) {
                i = commentMapper.deleteComment(commentMapper.getParentId(comment.getId()));
            }
            i = commentMapper.deleteComment(commentMapper.getId(comment.getId()));
            Comment root = commentMapper.getById(commentMapper.getByParentId(comment.getParentId()).getParentId());
            if(root != null && root.getParentId() == null) {
                root.setCommentNum(root.getCommentNum() - 1);
                commentMapper.updateLikeCount(root.getCommentNum(), root.getId());
            }
            if(!StringUtils.isEmpty(comment.getParentId()) && !comment.getParentId().equals(comment.getFromId())) {
                Comment father = commentMapper.getById(comment.getFromId()); //数据库改类型!!!!!!!!!!!!!!!!!!!!!
                if (father != null) {
                    father.setCommentNum(father.getCommentNum() - 1);
                    commentMapper.updateLikeCount(father.getCommentNum(), father.getId());
                }
                if(root != null && root.getParentId() == null) {
                    root.setCommentNum(root.getCommentNum() - 1);
                    commentMapper.updateLikeCount(root.getCommentNum(), root.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (i > 0){
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param current
     * @param parent
     * @return
     */
    @Override
    public boolean addComment(Comment current, Comment parent, Long currentId) {
        int i = 0;
        try{
            if (current != null && !StringUtils.isEmpty(current.getParentId())) {
                if (currentId.equals(current.getUserId())) {
                    i = commentMapper.addComment(current);
                }
            }
            if(parent != null) {
                commentMapper.updateLikeCount(parent.getCommentNum(), parent.getId());
                Comment root = commentMapper.getById(parent.getParentId());
                if(root != null && root.getParentId() == null) {
                    root.setCommentNum(root.getCommentNum() + 1);
                    commentMapper.updateLikeCount(root.getCommentNum(), root.getId());
                }

                if (currentId.equals(current.getUserId())) {
                    i = commentMapper.addComment(current);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        if (i > 0){
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param comment
     * @return
     */
    @Override
    public boolean updateLikeCount(Comment comment) {
        int i =  commentMapper.updateComment(comment);
        if (i > 0){
            return true;
        } else {
            return false;
        }
    }
}
